<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\pay\controller;

use app\pay\model\GoodsModel;
use cmf\controller\AdminBaseController;
use think\db\Query;
use think\facade\Db;

class ProductsController extends AdminBaseController
{
    /**
     * 产品分类
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function category()
    {
        $where['type'] = 1;
        $list = Db::name("category")->where($where)->order("id asc")->select()->toArray();
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function addCategory()
    {
        return $this->fetch();
    }

    /**
     * Notes:执行添加vip
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function addPostCategory()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['type'] = 1;
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = Db::name("category")->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:编辑vip页面
     * User:
     * DateTime: 2022-01-17 17:11
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editCategory()
    {
        $param = $this->request->param();
        $info = Db::name("category")->where(["id" => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行编辑vip
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function editPostCategory()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['update_time'] = date("Y-m-d H:i:s");
        $res = Db::name("category")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function deleteCategory()
    {
        $this->deleteInfo("category");
        $this->success("删除成功");
    }

    /**
     * Notes:商品列表
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $list = Db::name("products")
            ->where(function (Query $query) use ($param) {
                $query->where(['delete_time' => 0]);
                if (isset($param['title']) && !empty($param['title'])) {
                    $query->where('title', 'like', "%" . $param['title'] . "%");
                }
                if (isset($param['status']) && !empty($param['status'])) {
                    $query->where(['status' => $param['status']]);
                }
            })
            ->order("id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('title', isset($param['title']) ? $param['title'] : '');
        $this->assign('status', isset($param['status']) ? $param['status'] : '');
        return $this->fetch();
    }

    /**
     * Notes:添加商品页面
     * User:
     * DateTime: 2022-01-17 16:53
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * Notes:执行添加商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function addPost()
    {
        $param = $this->request->param();
        $data['title'] = $param['title'];
        $data['desc'] = $param['desc'];
        $data['image'] = $param['image'];
        $data['photos'] = $param['photos'];
        $data['content'] = $param['content'];
        $data['status'] = $param['status'];
        $data['price'] = $param['price'];
        $data['stock'] = $param['stock'];
        $data['sales'] = $param['sales'];
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = Db::name("category")->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:编辑商品页面
     * User:
     * DateTime: 2022-01-17 17:11
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $param = $this->request->param();
        $info = Db::name("category")->where(["id" => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行编辑商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function editPost()
    {
        $param = $this->request->param();
        $data['title'] = $param['title'];
        $data['desc'] = $param['desc'];
        $data['image'] = $param['image'];
        $data['photos'] = $param['photos'];
        $data['content'] = $param['content'];
        $data['status'] = $param['status'];
        $data['price'] = $param['price'];
        $data['stock'] = $param['stock'];
        $data['sales'] = $param['sales'];
        $data['update_time'] = time();
        $res = Db::name("category")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:启用/停用
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\db\exception\DbException
     */
    public function publish()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
            $id = $this->request->param('id', 0, 'intval');
            Db::name("category")->where('id', $id)->update(['status' => $param['status'], 'update_time' => time()]);
            $this->success("成功！", '');
        }

        if (isset($param['ids']) && isset($param["yes"])) {
            $ids = $this->request->param('ids/a');
            Db::name("category")->where('id', 'in', $ids)->update(['status' => 1, 'update_time' => time()]);
            $this->success("成功！", '');
        }
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function delete()
    {
        $this->deleteInfo("category");
        $this->success("删除成功");
    }

}
